package com.yuntian.controller.admin;

import com.yuntian.Dto.AdminOrderPageQueryDto;
import com.yuntian.Dto.OrderPageQueryDto;
import com.yuntian.Vo.admin.OrderVO;
import com.yuntian.entity.Order;
import com.yuntian.result.PageResult;
import com.yuntian.result.Result;
import com.yuntian.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 查询所有订单
     */
    @GetMapping("/all")
    public Result<PageResult> getAllOrders(OrderPageQueryDto orderPageQueryDto) {
        PageResult pageResult = orderService.selectAllOrder(orderPageQueryDto);
        return Result.success(pageResult);
    }

    /**
     * 根据用户ID查询订单
     */
    @GetMapping("/user/{userId}")
    public Result<PageResult> getOrdersByUserId(@PathVariable Long userId) {
        AdminOrderPageQueryDto adminOrderPageQueryDto = new AdminOrderPageQueryDto();
        adminOrderPageQueryDto.setUserId(userId);
        PageResult orders = orderService.selectOrderByUserId(adminOrderPageQueryDto);
        return Result.success(orders);
    }

    /**
     * 根据用户名查询订单
     */
    @GetMapping("/username")
    public Result<List<OrderVO>> getOrdersByUserName(@RequestParam String userName) {
        List<OrderVO> orderVOS = orderService.selectOrderByUserName(userName);
        return Result.success(orderVOS);
    }


    /**
     * 删除订单
     */
    @DeleteMapping("/delete/{orderId}")
    public Result deleteOrder(@PathVariable Integer orderId) {
        // 调用orderService的deleteOrder方法，删除指定id的订单
        orderService.deleteOrder(orderId);
        // 返回成功结果
        return Result.success();
    }

    /**
     * 订单状态更新
     */
    @PutMapping("/updateStatus")
    public Result updateOrderStatus(@RequestBody Order order) {
        orderService.updateOrderStatus(order);
        return Result.success();
    }

    /**
     * 统计订单数量
     */
    @GetMapping("/count")
    public Result<Integer> count(){
        Integer count = orderService.count();
        return Result.success(count);
    }





}
